package com.cx.service.impl;

import com.cx.mapper.PraiseMapper;
import com.cx.pojo.Praise;
import com.cx.pojo.PraiseExample;
import com.cx.service.PraiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class
PraiseServiceImpl implements PraiseService {

    @Autowired
    PraiseMapper praiseMapper;


    @Override
    public Integer insertPraise(Praise praise) {
        Integer rows = praiseMapper.insertSelective(praise);
        return rows;
    }

    @Override
    public Integer deletePraise(Integer praiseId) {
        Integer rows = praiseMapper.deleteByPrimaryKey(praiseId);
        return rows;
    }

    @Override
    public Integer deletePraiseByPostId(Integer postId) {
        PraiseExample praiseExample = new PraiseExample();
        PraiseExample.Criteria criteria = praiseExample.createCriteria();
        criteria.andPostIdEqualTo(postId);
        Integer rows = praiseMapper.deleteByExample(praiseExample);
        return rows;
    }

    @Override
    public Integer deletePraiseByOther1(Integer postId, String other1) {
        PraiseExample praiseExample = new PraiseExample();
        PraiseExample.Criteria criteria = praiseExample.createCriteria();
        criteria.andPostIdEqualTo(postId);
        criteria.andOther1EqualTo(other1);
        Integer rows = praiseMapper.deleteByExample(praiseExample);
        return rows;
    }

    @Override
    public Integer updatePost(Integer praiseId, Praise praise) {
        PraiseExample praiseExample = new PraiseExample();
        PraiseExample.Criteria criteria = praiseExample.createCriteria();
        criteria.andPraiseIdEqualTo(praiseId);
        Integer rows = praiseMapper.updateByExampleSelective(praise,praiseExample);

        return rows;
    }

    @Override
    public List<Praise> selectByPostId(Integer postId) {
        PraiseExample praiseExample = new PraiseExample();
        PraiseExample.Criteria criteria = praiseExample.createCriteria();
        criteria.andPostIdEqualTo(postId);
        List<Praise> praiseList = praiseMapper.selectByExample(praiseExample);

        return praiseList;
    }

    @Override
    public List<Praise> selectPostByUserName(String userName) {
        PraiseExample praiseExample = new PraiseExample();
        PraiseExample.Criteria criteria = praiseExample.createCriteria();
        criteria.andOther1EqualTo(userName);
        List<Praise> praises = praiseMapper.selectByExample(praiseExample);
        return praises;
    }

    @Override
    public List<Praise> selectPostByPostIdAndUserName(Integer postId, String userName) {
        PraiseExample praiseExample = new PraiseExample();
        PraiseExample.Criteria criteria = praiseExample.createCriteria();
        criteria.andOther1EqualTo(userName);
        criteria.andPostIdEqualTo(postId);
        List<Praise> praises = praiseMapper.selectByExample(praiseExample);
        return praises;
    }
}
